using FytSoa.Application.Cm.Param;
using FytSoa.Domain.Cm;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Cm;

/// <summary>
/// 社区内容服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v6")]
public class CmCommunityService : IApplicationService 
{
    private readonly SugarRepository<CmCommunity> _thisRepository;
    public CmCommunityService(SugarRepository<CmCommunity> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CmCommunityDto>> GetPagesAsync(CommunityWhereParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Status=="1",m=>m.Status)
            .WhereIF(param.IsTop==1,m=>m.IsTop)
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Title.Contains(param.Key) || m.Summary.Contains(param.Key))
            .Includes(m=>m.User)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<CmCommunityDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CmCommunityDto> GetAsync(long id)
    {
        var model = await _thisRepository
            .AsQueryable()
            .Includes(m=>m.User)
            .FirstAsync(m=>m.Id==id);
        return model.Adapt<CmCommunityDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(CmCommunityAddDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<CmCommunity>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CmCommunityAddDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CmCommunity>());
    
    /// <summary>
    /// 审核
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAuditAsync(CmCommunityAuditParam param)=> 
        await _thisRepository.UpdateAsync(m => new CmCommunity()
        {
            Status = param.Audit
        },m=>param.Ids.Contains(m.Id));
    
    /// <summary>
    /// 推荐
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<bool> ModifyTopAsync(CmCommunityAuditParam param)=> 
        await _thisRepository.UpdateAsync(m => new CmCommunity()
        {
            IsTop = param.Audit
        },m=>param.Ids.Contains(m.Id));

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
